package com.wyvoid.forum.web.action.admin.usergroup;

import com.wyvoid.forum.web.action.admin.BaseAdminAction;
import com.wyvoid.forum.service.IUserService;
import com.wyvoid.forum.domain.User;
import com.wyvoid.forum.domain.UserProfile;
import com.wyvoid.core.persistent.Pagination;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import org.apache.commons.lang.StringUtils;

/**
 * Created by IntelliJ IDEA.
 * User: new
 * Date: 2007-7-18
 * Time: 15:34:18
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding(value = "/admin/editUsers.do")
public class EditUsersAction extends BaseAdminAction {
    @SpringBean
    private IUserService userService;
    private HibernatePage<User> userList;
    private String userName;
    private User user;
    private UserProfile userProfile;
    private long userId;

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }


    public HibernatePage<User> getUserList() {
        return userList;
    }

    public void setUserList(HibernatePage<User> userList) {
        this.userList = userList;
    }


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserProfile getUserProfile() {
        return userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }


    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    @DefaultHandler
     public Resolution findUserList(){
        if(StringUtils.isBlank(userName)){
        Pagination pagination=new Pagination();
        pagination.setPageNumber(1);
        pagination.setPageSize(10);
        userList=userService.getAllUser(pagination);
        }else{
            userProfile=userService.getUserProfileByUserName(userName);
        }
        return getAdminResolution("usergroup/findUsers");
    }


    public Resolution detaiUserDataForm(){
        user = userService.getUser(userId);
        userProfile = userService.getUserProfile(userId);
        return getAdminResolution("usergroup/detailUserData");
    }

    public Resolution detaiUserData(){
        User updateUser=userService.getUser(user.getId());
        UserProfile updateUserProfile=userService.getUserProfile(userProfile.getId());
        updateUser.setUsername(user.getUsername());
        updateUser.setNickname(user.getNickname());
        updateUser.setSex(user.getSex());
        updateUser.setEmail(user.getEmail());
        updateUser.setTotalTopic(user.getTotalTopic());
        updateUser.setPrimes(user.getPrimes());
        updateUser.setRegisterTime(user.getRegisterTime());
        updateUser.setLastLoginTime(user.getLastLoginTime());
        updateUser.setLastLoginIp(user.getLastLoginIp());
        updateUser.setPhotoUrl(user.getPhotoUrl());

        
        updateUserProfile.setWebsite(userProfile.getWebsite());
        updateUserProfile.setImQq(userProfile.getImQq());
        updateUserProfile.setImOther(userProfile.getImOther());
        updateUserProfile.setImMsn(userProfile.getImMsn());
        updateUserProfile.setAddress(userProfile.getAddress());
        updateUserProfile.setBirthDay(userProfile.getBirthDay());
        updateUserProfile.setIntroduce(userProfile.getIntroduce());
        userService.updateUserAndProfile(updateUser,updateUserProfile);
        return new RedirectResolution("/forum/admin/editUsers.do", false);
    }

    


    
}
